/**
 * 公告条
 */
class NoticeView extends BaseSprite {
    private horn: eui.Image;
    private group: eui.Group;
    private groupMask: egret.Shape;

    private list: any[] = [];
    private isTweenning: boolean;

    constructor() {
        super();

        this.skinName = "NoticeViewSkin";
    }

    childrenCreated() {
        super.childrenCreated();

        this.touchChildren = false;
        this.touchEnabled = false;

        this.groupMask = new egret.Shape();
        this.groupMask.graphics.clear();
        this.groupMask.graphics.beginFill(0, 1);
        this.groupMask.graphics.drawRect(65, 0, this.width - 80, this.height);
        this.groupMask.graphics.endFill();
        this.addChild(this.groupMask);

        this.group.mask = this.groupMask;
    }

    playList(texts: string[], color: number = 0x40f8ff, size: number = 20) {
        for (let i: number = 0; i < texts.length; i++) {
            this.play(texts[i], color, size);
        }
    }

    play(text: string, color: number = 0xfcffb7, size: number = 24) {
        let label: eui.Label = FactoryUtils.createLabel();
        label.verticalCenter = 0;
        label.textColor = color;
        label.stroke = 3;
        label.strokeColor = 0x00353a;
        label.size = size;
        label.text = text;
        this.list.push(label);

        console.log('notice===>', text);

        this.show();

        if (this.list.length != 1) {
            this.playing(label);
        } else {
            this.isTweenning = true;
            this.width = 0;
            this.horn.visible = false;

            let thiz = this;
            egret.Tween.get(this).to({width: 580}, 300).call(function () {
                thiz.isTweenning = false;
                thiz.horn.visible = true;
                thiz.playing(label);
            });
        }
    }

    private playing(label: eui.Label | egret.TextField) {

        if (this.isTweenning)return;

        this.isTweenning = true;

        label.x = this.group.width;
        this.group.addChild(label);

        let thiz = this;
        egret.Tween.get(label).to({x: -label.width}, (this.group.width + label.width) * 10).call(function () {
            thiz.group.removeChildren();
            thiz.list.shift();
            thiz.isTweenning = false;
            if (thiz.list.length) {
                thiz.playing(thiz.list[0]);
            } else {
                thiz.ending();
            }
        });
    }

    private ending() {
        this.horn.visible = false;

        let thiz = this;
        egret.Tween.get(this).to({width: 0}, 300).call(function () {
            thiz.hide();
        });
    }

    show() {
        super.show();

        this.horizontalCenter = 0;
        this.top = 98;

        LayerManager.i.topLayer.addChild(this);
    }

    hide() {
        super.hide();

        if (LayerManager.i.topLayer.contains(this)) {
            LayerManager.i.topLayer.removeChild(this);
        }
    }
}